package com.xavier.dong.nacos.grpc.server.config;

import com.xavier.dong.nacos.grpc.server.interceptor.LogGrpcInterceptor;
import io.grpc.ServerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
import org.springframework.context.annotation.Configuration;

/**
 * Example configuration class that adds a {@link ServerInterceptor} to the global interceptor list.
 *
 * @author <a href="mailto:xing.dong@sha.ontime-express.com">Xavier Dong</a>
 * @date
 */
@Configuration(proxyBeanMethods = false)
// proxyTargetClass is required, if you use annotation driven security!
// However, you will receive a warning that GrpcServerService#bindService() method is final.
// You cannot avoid that warning (without massive amount of work), but it is safe to ignore it.
// The #bindService() method uses a reference to 'this', which will be used to invoke the methods.
// If the method is not final it will delegate to the original instance and thus it will bypass any security layer that
// you intend to add, unless you re-implement the #bindService() method on the outermost layer (which Spring does not).
//@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true)
@Slf4j
public class GlobalInterceptorConfiguration {

    /**
     * Creates a new {@link LogGrpcInterceptor} bean and adds it to the global interceptor list. As an alternative you
     * can directly annotate the {@code LogGrpcInterceptor} class and it will automatically be picked up by spring's
     * classpath scanning.
     *
     * @return The newly created bean.
     */
    @GrpcGlobalServerInterceptor
    LogGrpcInterceptor logServerInterceptor() {
        return new LogGrpcInterceptor();
    }

    ///**
    // * 密码编码器
    // *
    // * @return {@link PasswordEncoder}
    // */
    //@Bean
    //PasswordEncoder passwordEncoder() {
    //    return new BCryptPasswordEncoder(10);
    //}
    //
    ///**
    // * This could be your database lookup. There are some complete implementations in spring-security-web.
    // *
    // * @param passwordEncoder 密码编码器
    // * @return {@link UserDetailsService}
    // */
    //@Bean
    //UserDetailsService userDetailsService(final PasswordEncoder passwordEncoder) {
    //    return username -> {
    //        log.debug("Searching user: {}", username);
    //        switch (username) {
    //            case "guest": {
    //                return new User(username, passwordEncoder.encode(username + "Password"), Collections.emptyList());
    //            }
    //            case "user": {
    //                final List<SimpleGrantedAuthority> authorities =
    //                        Collections.singletonList(new SimpleGrantedAuthority("ROLE_GREET"));
    //                return new User(username, passwordEncoder.encode(username + "Password"), authorities);
    //            }
    //            default: {
    //                throw new UsernameNotFoundException("Could not find user!");
    //            }
    //        }
    //    };
    //}
    //
    //
    ///**
    // * One of your authentication providers.
    // * They ensure that the credentials are valid and populate the user's authorities.
    // *
    // * @param userDetailsService 用户详细信息服务
    // * @param passwordEncoder    密码编码器
    // * @return {@link DaoAuthenticationProvider}
    // */
    //@Bean
    //DaoAuthenticationProvider daoAuthenticationProvider(
    //        final UserDetailsService userDetailsService,
    //        final PasswordEncoder passwordEncoder) {
    //
    //    final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    //    provider.setUserDetailsService(userDetailsService);
    //    provider.setPasswordEncoder(passwordEncoder);
    //    return provider;
    //}
    //
    //
    ///**
    // * 身份验证管理器
    // * <p>
    // * Add the authentication providers to the manager.
    // *
    // * @param daoAuthenticationProvider 刀身份验证提供者
    // * @return {@link AuthenticationManager}
    // */
    //@Bean
    //AuthenticationManager authenticationManager(final DaoAuthenticationProvider daoAuthenticationProvider) {
    //    final List<AuthenticationProvider> providers = new ArrayList<>();
    //    providers.add(daoAuthenticationProvider);
    //    return new ProviderManager(providers);
    //}
    //
    ///**
    // * Configure which authentication types you support.
    // *
    // * @return {@link GrpcAuthenticationReader}
    // */
    //@Bean
    //GrpcAuthenticationReader authenticationReader() {
    //    return new BasicGrpcAuthenticationReader();
    //    // final List<GrpcAuthenticationReader> readers = new ArrayList<>();
    //    // readers.add(new BasicGrpcAuthenticationReader());
    //    // readers.add(new SSLContextGrpcAuthenticationReader());
    //    // return new CompositeGrpcAuthenticationReader(readers);
    //}

}